//
//  com_tskiViewController.m
//  12coredata
//
//  Created by Rocky Xu on 12-8-31.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "com_tskiViewController.h"
#import "com_tskiAppDelegate.h"
#import <CoreData/CoreData.h>
@implementation com_tskiViewController


@synthesize line1;
@synthesize line2;
@synthesize line3;
@synthesize line4;

- (void)didReceiveMemoryWarning
{
    
    [super didReceiveMemoryWarning];
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    
    com_tskiAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];	
    NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Line" inManagedObjectContext:context];
    NSFetchRequest *request = [[NSFetchRequest alloc]init];
    [request setEntity:entityDesc];
    
    NSError *error;
    NSArray *objects = [context executeFetchRequest:request error:&error];
    if(objects == nil){
        NSLog(@"objects is nil");
    }
    
    for(NSManagedObject *oneObject in objects){
        NSNumber *lineNum = [oneObject valueForKey:@"line_num"];
        NSString *lineText = [oneObject valueForKey:@"line_text"];
        
        NSString *fieldName = [NSString stringWithFormat:@"line%d",[lineNum integerValue]];
        UITextField *field = [self valueForKey:fieldName];
        field.text = lineText;
    }
    [request release];
    
    UIApplication *app =[UIApplication sharedApplication];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(applicationWillResignActive:) name:UIApplicationWillResignActiveNotification object:app];
    
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
}


-(void)applicationWillResignActive:(NSNotification *)notificaton{
    com_tskiAppDelegate *appDelegate = [[UIApplication sharedApplication] delegate];
    NSManagedObjectContext *context = [appDelegate managedObjectContext];	
    NSError *error;
    for(int i=1;i<=4;i++){
        NSString *fieldName = [NSString stringWithFormat:@"line%d",i];
        UITextField *field = [self valueForKey:fieldName];
        
        NSEntityDescription *entityDesc = [NSEntityDescription entityForName:@"Line" inManagedObjectContext:context];
        NSFetchRequest *request = [[NSFetchRequest alloc]init];
        [request setEntity:entityDesc];
        
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"(line_num = %d)",i];
        [request setPredicate:pred];
        
        NSManagedObject *theLine = nil;
        NSArray *objects = [context executeFetchRequest:request error:&error];
        if(objects == nil){
            NSLog(@"objects is nil");
        }
        if([objects count] > 0)
            theLine = [objects objectAtIndex:0];
        else
            theLine = [NSEntityDescription insertNewObjectForEntityForName:@"Line" inManagedObjectContext:context];
        [theLine setValue:[NSNumber numberWithInt:i] forKey:@"line_num"];
        [theLine setValue:field.text forKey:@"line_text"];
        [request release];
    }
    [context save:&error ];
    
}


- (void)viewDidUnload
{
    self.line1 = nil;
    self.line2 = nil;
    self.line3 = nil;
    self.line4 = nil;
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

-(void)dealloc{
    [line1 release];
    [line2 release];
    [line3 release];
    [line4 release];
    [super dealloc];
}
- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

@end
